﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Function
{
    public partial class FunctionForm : Form
    {
        Form Index;
        bool isExit = true;

        public FunctionForm()
        {
            InitializeComponent();
        }
        public FunctionForm(Form Index)
        {
            InitializeComponent();
            this.Index = Index;
        }

        private void FunctionForm_Load(object sender, EventArgs e)
        {

        }

        private void FunctionForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isExit) Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Index.Show();
            Index.Location = this.Location;
            isExit = false;
            this.Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            // 统计中文、英文、数字、标点符号、空格、换行符的个数
            string str = textBox1.Text;
            int chinese = 0, english = 0, number = 0, punctuation = 0, space = 0, line = 0;
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] >= 0x4E00 && str[i] <= 0x9FA5) // 中文
                {
                    chinese++;
                }
                else if ((str[i] >= 0x0041 && str[i] <= 0x005A) || (str[i] >= 0x0061 && str[i] <= 0x007A)) // 英文
                {
                    english++;
                }
                else if (str[i] >= 0x0030 && str[i] <= 0x0039) // 数字
                {
                    number++;
                }
                // 先判断是否是空格，再判断是否是换行符，剩下的都当作标点符号
                else if (str[i] == 0x0020)
                {
                    space++;
                }
                else if (str[i] == 0x000A||str[i] == 0x000D)
                {
                    // \r\n算作一个换行符,仅统计\n
                    if (str[i] == 0x000A) line++;
                }
                else
                {
                    punctuation++;
                }
                label1.Text = "中文：" + chinese;
                label2.Text = "英文：" + english;
                label3.Text = "数字：" + number;
                label4.Text = "标点符号：" + punctuation;
                label5.Text = "空格：" + space;
                label6.Text = "换行符：" + line;
            }
        }
    }
}